package com.pointlion.sys.mvc.admin.evaluate.paper;

import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.pointlion.sys.mvc.common.model.Paper;
import com.pointlion.sys.plugin.shiro.ShiroKit;

import java.util.List;

public class PaperService{
	public static final PaperService me = new PaperService();
	public static final String TABLE_NAME = Paper.tableName;
	
	/***
	 * ����������ѯ
	 */
	public Paper getById(String id){
		return Paper.dao.findById(id);
	}
	
	/***
	 * ��ȡ��ҳ
	 */
	public Page<Record> getPage(int pnum,int psize){
		String sql  = " from "+TABLE_NAME+" o order by o.create_time desc";
		return Db.paginate(pnum, psize, " select * ", sql);
	}
	
	/***
	 * ɾ��
	 * @param ids
	 */
	@Before(Tx.class)
	public void deleteByIds(String ids){
    	String idarr[] = ids.split(",");
    	for(String id : idarr){
    		Paper o = me.getById(id);
			Db.update("delete from paper_user where paper_id = '"+id+"'");
			Db.update("delete from paper_item where paper_id = '"+id+"'");
    		o.delete();
    	}
	}

    public List<Paper> getPubPaper() {
		String sql = "select p.*,a.create_id from paper p " +
				" left JOIN (select * from paper_user where create_id = '"+ShiroKit.getUserId()+"' group by paper_id " +
				") a ON p.id = a.paper_id  " +
				"where paperStatus = 1 and DATE_FORMAT(NOW(),'%Y-%m-%d') <= p.paperEndDate";
		return Paper.dao.find(sql);
    }

	public List<Paper> getHistroyPaper() {
		String sql = "select p.*,u.create_id,round(sum(total_value),2) total from paper_user u " +
				" left join paper p on p.id = u.paper_id " +
				" where u.create_id = '"+ShiroKit.getUserId()+"' group by u.paper_id ";
		return Paper.dao.find(sql);
	}
}